package cn.danger.com.model.action;

import android.content.Context;
import android.text.TextUtils;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.danger.com.model.entity.MyUser;
import cn.danger.com.model.entity.RegisterUserInfo;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/27.
 */
public class RegisterModelImpl implements RegisterModel {

    private Context context;

    public RegisterModelImpl(Context context) {
        this.context = context;
    }

    private String check(RegisterUserInfo userInfo) {
        if (TextUtils.isEmpty(userInfo.getPhone())) {
            return Constants.PHONE_IS_EMPTY;
        }
        if (!Check.isMobileNO(userInfo.getPhone())) {
            return Constants.PHONE_IS_ILLEGAL;
        }
        if (TextUtils.isEmpty(userInfo.getCheck_code())) {
            return Constants.CHECK_CODE_IS_EMPTY;
        }
        if (TextUtils.isEmpty(userInfo.getPassword())) {
            return Constants.PASSWORD_IS_EMPTY;
        }
        if (userInfo.getPassword().length() < 6) {
            return Constants.PASSWORD_IS_NO_LESS_THAN_SIX;
        }
        if (TextUtils.isEmpty(userInfo.getConfirm_password())) {
            return Constants.CONFIRM_PASSWORD_IS_EMPTY;
        }
        if (!userInfo.getPassword().equals(userInfo.getConfirm_password())) {
            return Constants.CONFIRM_PASSWORD_IS_WRONG;
        }
        return Constants.IS_RIGHT;
    }

    @Override
    public void sendCheckCode(String phone, final OnCommonListener listener) {
        if (TextUtils.isEmpty(phone)) {
            listener.onError(Constants.PHONE_IS_EMPTY);
        }
        if (!Check.isMobileNO(phone)) {
            listener.onError(Constants.PHONE_IS_ILLEGAL);
        }
        if (!new MyNetManager(context).isNetworkAvailable()) {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        } else {
            BmobSMS.requestSMSCode(phone, "Schedule", new QueryListener<Integer>() {
                @Override
                public void done(Integer integer, BmobException e) {
                    if (e == null) {
                        listener.onSuccess(null);
                    } else {
                        e.printStackTrace();
                        if (Check.checkByCode(e.getErrorCode()) == null) {
                            listener.onError(Constants.FAIL_TO_SEND_CHECK);
                        } else {
                            listener.onError(Check.checkByCode(e.getErrorCode()));
                        }
                    }
                }
            });
        }
    }

    @Override
    public void register(RegisterUserInfo userInfo, final OnCommonListener listener) {
        String check_result = check(userInfo);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                MyUser user = new MyUser();
                user.setUsername(userInfo.getPhone());
                user.setPassword(userInfo.getPassword());
                user.setMobilePhoneNumber(userInfo.getPhone());
                user.setMobilePhoneNumberVerified(true);
                user.signOrLogin(userInfo.getCheck_code(), new SaveListener<MyUser>() {
                    @Override
                    public void done(MyUser myUser, BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            e.printStackTrace();
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_REGISTER);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            }
        } else {
            listener.onError(check_result);
        }
    }
}
